//
//  ShopcartHomeTableViewCell.m
//  B2C
//
//  Created by 李小龙 on 2017/8/8.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "ShopcartHomeTableViewCell.h"

@interface ShopcartHomeTableViewCell ()

@end

@implementation ShopcartHomeTableViewCell
- (IBAction)subButtonAction:(id)sender {
    [self.editCountTextField resignFirstResponder];
    if (self.editBlock) {
        self.editBlock(EditTypeSub);
    }
}
- (IBAction)addButtonAction:(id)sender {
    [self.editCountTextField resignFirstResponder];
    if (self.editBlock) {
        self.editBlock(EditTypeAdd);
    }
}
- (IBAction)specsButtonAction:(id)sender {
    [self.editCountTextField resignFirstResponder];
    if (self.editBlock) {
        self.editBlock(EditTypeSpecs);
    }
}
- (IBAction)deleteButtonAction:(id)sender {
    [self.editCountTextField resignFirstResponder];
    if (self.editBlock) {
        self.editBlock(EditTypeDelete);
    }
}
- (IBAction)selectButtonAction:(id)sender {
    [self.editCountTextField resignFirstResponder];
    [_selectButton setSelected:!_selectButton.selected];
    if (self.editBlock) {
        self.editBlock(EditTypeSelect);
    }
}

- (void)updateCellWithModel:(ShopcartModel *)model {
    // 库存
    self.maxCount = model.gastock.integerValue;
    // 是否选中
    [self.selectButton setSelected:model.isSelect];
    // 商品图片
    [self.goodsImageView sd_setImageWithURL:GetUrl(model.gimg) placeholderImage:PlaceholderImagePictures];
    
    // 商品名
    [self.normalNameLabel setText:model.gname];
    // 价格
    if (TextIsNotNil(model.gaoldprice)) {
        [self.normalPriceLabel setText:[NSString stringWithFormat:@"¥%@",model.gaprice]];
        
        NSString *oldStr = [NSString stringWithFormat:@" ¥%@ ",model.gaoldprice];
        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:oldStr];
        
        [attributeStr addAttributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|NSUnderlinePatternSolid),
                                      NSStrokeColorAttributeName:[UIColor gray66Color]} range:NSMakeRange(0, oldStr.length)];
        [self.normalOldPriceLabel setAttributedText:attributeStr];
    } else {
        [self.normalPriceLabel setText:[NSString stringWithFormat:@"¥%@",model.gaprice]];
        [self.normalOldPriceLabel setText:@""];
    }
    // 数量
    [self.normalCountLabel setText:[NSString stringWithFormat:@"x%@",model.number]];
    // 属性名拼接
    GoodsAttributePriceModel *temp;
    for (GoodsAttributePriceModel *attModel in model.goodsattribute) {
        if ([model.gaid isEqualToString:attModel.gaid]) {
            temp = attModel;
            break;
        }
    }
    NSString *specs = @"";
    if (temp) {
        for (NSInteger i = 0; i < temp.twoattribute.count; i++) {
            TwoAtrributeModel *twoAtt = temp.twoattribute[i];
            if (i == 0) {
                specs = twoAtt.taname;
            } else {
                specs = [NSString stringWithFormat:@"%@;%@",specs,twoAtt.taname];
            }
        }
    }
    [self.normalSpecsLabel setText:specs];
    // 数量
    [self.editCountTextField setText:model.number];
    // 属性名拼接
    [self.editSpecsLabel setText:specs];
}

- (void)textFieldTextDidChange:(NSNotification *)info {
    UITextField *textField = (UITextField *)info.object;
    if (textField == self.editCountTextField) {
        if ([textField.text isEqualToString:@""]) {
            [textField setText:@"0"];
        }
        if (self.maxCount > 0) {
            NSInteger count = [textField.text integerValue];
            if (count > self.maxCount) {
                count = self.maxCount;
                [MBProgressHUD myShowHUDAddedTo:[UIApplication sharedApplication].keyWindow labelText:@"数量超出库存~" animated:YES];
                [textField setText:[NSString stringWithFormat:@"%ld", (long)count]];
                if (self.editBlock) {
                    self.editBlock(EditTypeTextField);
                }
            } else {
                [textField setText:[NSString stringWithFormat:@"%ld", (long)count]];
                
                if (self.editBlock) {
                    self.editBlock(EditTypeTextField);
                }
            }
        } else {
            [MBProgressHUD myShowHUDAddedTo:[UIApplication sharedApplication].keyWindow labelText:@"数量超出库存~" animated:YES];
            if (self.editBlock) {
                self.editBlock(EditTypeTextField);
            }
        }
        
    }
    
}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:self.editCountTextField];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:self.editCountTextField];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
